<?php

if(!defined('BASEPATH')) exit('No direct script access allowed');
class Pager
{
    public $length = 5;
    public $prevLabel = '';
    public $nextLabel = '';
    public $first = 1; //第一页
    public $slider = 3;
    public $perpage = 3;
    public $query;
    public $table;
    public $count;
    public $obj;
    public $current;
    public $id;
    public $bools;
    public $boolPgae;
    /**
     * table:查询的表,$segment:当前URI中第几位是分页号,$perpage:每页显示的数量
     */ 
    function init(){ 
      
    }


    //使用AR
    /**
     * $total_count:总数量,$data=>where语句,
     * $desc:排序
     * $desc_seq:URI中第几个字段是排序符(eg:id_desc)
     */ 
    function ar($total_count, $where = null,$table = '', $segment = 5,$perpage =5)
    {
        $CI = &get_instance();
        $this->table = $table;
        $this->current = $CI->uri->segment($segment); 
        $this->perpage = $perpage;
        $this->count = $total_count;
        $this->current=$this->current>0?$this->current-1:0;
        $this->query = $CI->mcom_model->query_ByWhere_Limit($this->table, $where, $this->perpage, $this->perpage * $this->current);
        return $this;
    }


    function page($url, $current)
    {
        $perpage = $this->perpage;
        $last = ceil($this->count / $perpage)+1;
                    
        $prev = $current - 1; //上一页
        $next = $current + 1; //下一页
        $output = "";
          
              
        $page_url = $url . '/';

        if($current == $this->first) //第一页

        {
            $output .= "<span class='go_up'><a href='javascript:void(0)'>首 页 " . $this->prevLabel . "</a></span>";
        } else
        {
            $output .= "<span class='go_up'><a href='" . site_url($page_url) . "'>首 页  " . $this->prevLabel .
                "</a></span>";
        }

        $mid = intval($this->length / 2);
        if($current < $this->first)
        {

            $current = $this->first;

        }

        if($current > $last)
        {

            $current = $last;

        }
        $begin = $current - $mid;
         
        if($begin < $this->first)
        {
            $begin = $this->first;
        }

        $end = $begin + $this->length - 1;

        if($end >= $last)
        {

            $end = $last - 1;

            $begin = $end - $this->length + 1;

            if($begin < $this->first)
            {
                $begin = $this->first;
            }

        }
          
        if($begin > $this->first)
        {
                    
            for ($i = $this->first; $i < $this->first + $this->slider && $i < $begin; $i++)
            {

                $page = $i+1;
                $in = $i + 1;
                $urls = site_url($page_url . $page);  
                $output .= "<a href=\"{$urls}\">{$in}</a>";
            }
            if($i < $begin)
            {
                $output .= "<em>...</em>";
            }

        }
             
        for ($i = $begin; $i <= $end; $i++)
        {
            
            $page = $i;

            $in = $i ;
                 
            if($i == $current)
            {
               // $output .= "<a href='javascript' class=\"current\">{$in}</a>";
			    $output .= "<em class='current'>{$in}</em>";

            } else
            {               
                $urls = site_url($page_url . $page);
                $output .= "<a href=\"{$urls}\">{$in}</a>";

            }

        }

        if($last - $end > $this->slider)
        {

            $output .= "<em>...</em>";
            $end = $last - $this->slider;

        }


        for ($i = $end + 1; $i < $last; $i++)
        {

            $page = $i+1;
            $in = $i + 1;
            if($in!=$last){
              $urls = site_url($page_url . $page);      
              $output .= "<a href=\"{$urls}\">{$in}</a>";
            }
        }
                      
        if($current == $last - 1)
        {
          
            $output .= "<span class='go_down'><a href='javascript:void(0)'>" . $this->nextLabel . " 尾 页</a></span>";

        } else
        {
            $page = abs($last - 1);
            $urls = site_url($page_url . $page);
             
            $output .= "<span class='go_down'><a href=\"{$urls}\">" . $this->nextLabel . " 尾 页</a></span>";
        }
        //$output .= "</ul>";
       // $output .= "<li><span class='r_text'> 每页显示: ".$this->perpage." 条,总 ".$this->count." 条记录。 </span></li> </ul>";
        echo $output;
    }

}
